package com.example.jiemian.UI;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.jiemian.R;
import com.example.jiemian.bean.EventMessage;
import com.example.jiemian.bean.Luntan;
import com.example.jiemian.sqlite.LuntanDBUtils;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class AddActivity extends AppCompatActivity {
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    ImageView tvRight;
    @BindView(R.id.image_head)
    ImageView imageHead;
    @BindView(R.id.rl_head)
    LinearLayout rlHead;
    @BindView(R.id.tv_biaoti)
    EditText tvbiaoti;
    @BindView(R.id.tv_chengyuan)
    EditText tv_chengyuan;

    @BindView(R.id.tv_gequ)
    EditText tv_gequ;
    @BindView(R.id.tv_jianjie)
    EditText tv_jianjie;

    @BindView(R.id.btn_send)
    AppCompatTextView btnSend;
    String path= "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ButterKnife.bind(this);
        tvTitle.setText("发布乐队信息");
    }
    @OnClick({R.id.rl_back, R.id.rl_head, R.id.btn_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_head:
                PictureSelector
                        .create(AddActivity.this, PictureSelector.SELECT_REQUEST_CODE)
                        .selectPicture();
                break;
            case R.id.btn_send:

                String  chengyuan = tv_chengyuan.getText().toString();
                String tvbiaoti1 = tvbiaoti.getText().toString();
                String   jianjie = tv_jianjie.getText().toString();
                String  gequ = tv_gequ.getText().toString();
                if (TextUtils.isEmpty(tvbiaoti1)){
                    Toast.makeText(this, "请输入信息...", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    Luntan luntan = new Luntan();
                    luntan.setHead_url(MyApplication.getInstance().user.getHead_url());
                    luntan.setUsername(MyApplication.getInstance().user.getUsername());
                    luntan.setUser_id("1");
                    luntan.setPic(path);
                    luntan.setBiaoti(tvbiaoti1);
                    luntan.setLeixing(chengyuan);
                    luntan.setZan("0");
                    luntan.setContent(jianjie);
                    luntan.setTime(gequ);
                    luntan.setFabuzhe("乐队");
                    LuntanDBUtils.getInstance(getApplicationContext()).insert(luntan);
                    Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show();
                    EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
                    finish();
                }
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
            if (data != null) {
                PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                Glide.with(this).load(pictureBean.getPath()).into(imageHead);
                path = pictureBean.getPath();
            }
        }
    }
}